package com.blink.zookeeper.operate;

import com.blink.base.TestZK;
import com.blink.zookeeper.ZooKeeperCreator;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.jupiter.api.Test;

/**
 * <Description> 更新数据 ：数据修改后版本号会发生变化</Description>
 * <ClassName> TestZKSetData</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月12日 14:46
 */
public class TestZKSetData extends TestZK {

    @Test
    public void testSetDataSync() throws KeeperException, InterruptedException {
        ZooKeeper zk = ZooKeeperCreator.getZk("testSetDataSync");
        String path = "/testSetDataSync";
        zk.create(path, "中文".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        Stat stat = new Stat();
        byte[] data = zk.getData(path, false, stat);
        logger.info(new String(data));
        logger.info("{},{},{}", stat.getCzxid(), stat.getMzxid(), stat.getVersion());
        zk.setData(path, "123".getBytes(), -1);//-1表示不校验版本号
        byte[] data_ = zk.getData(path, false, stat);
        logger.info(new String(data_));
        logger.info("{},{},{}", stat.getCzxid(), stat.getMzxid(), stat.getVersion());
        zk.setData(path, "1234".getBytes(), -1);
        logger.info(new String(zk.getData(path, false, stat)));
        logger.info("{},{},{}", stat.getCzxid(), stat.getMzxid(), stat.getVersion());
        zk.setData(path, "1234".getBytes(), 1);//org.apache.zookeeper.KeeperException$BadVersionException: KeeperErrorCode = BadVersion
        Thread.sleep(Integer.MAX_VALUE);
    }

    @Test
    public void testSetDataASync() {

    }
}
